python利用opencv实现本地图片的识别与结果存储 您所在的位置:网站首页 python图片裁剪 定位 python利用opencv实现本地图片的识别与结果存储

python利用opencv实现本地图片的识别与结果存储

2023-03-10 08:46| 来源: 网络整理| 查看: 265

# This is a sample Python script.import os.path

# Press Shift+F10 to execute it or replace it with your code.# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.import cv2 as cvimport dlibimport numpy as npimport face_recognition

# 加载人脸检测器和关键点检测器detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# 已知人脸存储路径known_face_path = 'faces/gugu/'# 需要检测的人脸存储路径unknown_face_path = 'faces/gys/'# 记录结果record_face_path = 'faces/record/'

def recognize_image(): # 读取已知人脸,并解析 known_face_encodings = [] known_face_names = [] print('开始加载已知人脸数据...') for root, dirs, files in os.walk(known_face_path): total_process = len(files) process = 0 for f, file in enumerate(files): img = face_recognition.load_image_file(known_face_path + file) face_encodings = face_recognition.face_encodings(img, model='cnn') if len(face_encodings) > 0: known_face_encodings.append(face_encodings[0]) known_face_names.append(file[0:file.index('.')]) process += 1 print('----> 已加载' + str(process / total_process * 100 // 1) + '%') print('数据加载完成!')

# 识别人脸数据 if not os.path.exists(record_face_path): os.makedirs(record_face_path)

for root, dirs, files in os.walk(unknown_face_path): total_process = len(files) process = 0 for f, file in enumerate(files): cv_img = cv.imdecode(np.fromfile(unknown_face_path + file, dtype=np.uint8), cv.IMREAD_COLOR)

# img = face_recognition.load_image_file(unknown_face_path + file) img = cv.cvtColor(cv_img, cv.COLOR_BGR2RGB) locations = face_recognition.face_locations(img) face_encodings = face_recognition.face_encodings(img, locations, model='cnn') for (top, right, bottom, left), face_encoding in zip(locations, face_encodings): matches = face_recognition.compare_faces(known_face_encodings, face_encoding, .5) if True in matches: index = matches.index(True) name = known_face_names[index] # 绘制矩形框 cv.rectangle(cv_img, (left, top, right - left, bottom - top), (0, 255, 0), 2) cv.putText(cv_img, name, (left + 10, bottom - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv.imencode('.jpg', cv_img, [cv.IMWRITE_JPEG_QUALITY, 100])[1].tofile(record_face_path + '副本_' + file)

process += 1 print('----> 已识别' + str(process / total_process * 100 // 1) + '%')

# Press the green button in the gutter to run the script.if __name__ == '__main__': print_hi('PyCharm') recognize_image()



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有